<%--
  Created by IntelliJ IDEA.
  User: yutong
  Date: 14-4-24
  Time: 上午11:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="com.genghis.ptas.security.entity.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%
    User user = (User)session.getAttribute("user");
%>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    <%@include file="../includes/common.jsp" %>
    <%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
    <%@ page contentType="text/html;charset=UTF-8" %>

    <link rel="stylesheet" type="text/css"  href="<c:url value='/js/css/spigPet.css'/>"/>
    <title>查找好友</title>
</head>
    <body>
    <div style="text-align: center;margin-top: 20px;">
    <form class="form-inline">
        输入账号：<input type="text" class="input-medium" name="loginName" id="loginName"  value=""/>

        <a class="btn btn-primary" onclick="query()">查找</a>
    </form>
    </div>
        <div>
            <table id="dataTable"></table>
        </div>
    </body>
</html>
<script type="text/javascript">
    function query(){
        initDataGrid();
        listOption.url = basePath+"consumer/findOneFriend";
        listOption.data = "loginName="+$("#loginName").val().trim();
        listOption.pageNumber = 1;
        loadData(listOption);
    }
    function initDataGrid() {
        $('#dataTable').datagrid({
            title: "查找结果",
            pagination: true,
            fitColumns: true,
            columns: [
                [
                    {field: 'loginName', title: '用户名', sortable: true, align: 'center', width: 300},
                    {field: 'showSex', title: '性别', align: 'center', width: 150},
                    {field: 'city', title: '城市', align: 'center', width: 250},
                    {field: 'birthday', title: '生日', align: 'center', width: 300},
                    {field: 'email', title: '邮箱', align: 'center', width: 400},
                    {field: 'edit', title: '操作', align: 'center', width: 200}
                ]
            ],
            sortName: "",
            sortOrder: "",
            onSortColumn: onSortColumn
        });
    }
    function onSortColumn(sortColumn, sortDirection) {
        switch (sortColumn) {
            case "loginName":
                sortColumn = "loginName";
                break;

        }
        onSortColumnDefault(sortColumn, sortDirection);
    }
    function format(data) {
        data.resultList = queryFormat(data.resultList);
        return data;
    }
    function queryFormat(temp) {
        var rowDataList = [];
        for (var i in temp) {
                for(var t in ${friendList}){
                    var flag=false;
                   if(${friendList}[t]==temp[i].id){
                       flag=true;
                       break;
                   }
                }
            if(flag==true){
                temp[i].edit = "&nbsp;<a>已经关注</a>&nbsp;";

            }else if(temp[i].id==<%=user.getId()%>){
                temp[i].edit = "&nbsp;<a>本人</a>&nbsp;";
            }else{
                temp[i].edit = "&nbsp;<a onclick='addFriend(\"" + temp[i].id + "\")'>关注</a>&nbsp;";
            }
            rowDataList.push(temp[i]);
        }
        return rowDataList;
    }

    function addFriend(id) {
                $.ajax({
                    url: basePath + "consumer/addFriend",
                    data:{
                        friendId:id
                    } ,
                    dataType: "json",
                    type: "POST",
                    success: function () {
                        $.messager.alert("成功", "你已经成功关注他了！", "", function () {
                            parent.$("#dataEdit").dialog('close');
                            parent.query();
                        })
                    },
                    error: doError
                })
    }
</script>